<?php
/**
 * BlogForm class. This is where all the Admin side forms are generated
 */
class BlogForm extends PostBackControl
{
    var $ConfigurationManager;

    function BlogForm(&$Context)
    {
        $this->Name = 'BlogForm';
        $this->ValidActions = array('Blog','ProcessBlog');
	$this->Constructor($Context);

        if($this->IsPostBack)
        {
            $SettingsFile = $this->Context->Configuration['APPLICATION_PATH'].'conf/settings.php';
            $this->ConfigurationManager = $this->Context->ObjectFactory->NewContextObject($this->Context, 'ConfigurationManager');
            if ($this->PostBackAction == 'ProcessBlog')
            {
                $this->ConfigurationManager->GetSettingsFromForm($SettingsFile);
                $this->ConfigurationManager->DefineSetting('BLOG_EXCLUDE_FROM_DISCUSSION', ForceIncomingBool('BLOG_EXCLUDE_FROM_DISCUSSION', 0), 0);
                $this->ConfigurationManager->DefineSetting('BLOG_SOCIALBOOKMARKS_DIGG', ForceIncomingBool('BLOG_SOCIALBOOKMARKS_DIGG', 0), 0);
                $this->ConfigurationManager->DefineSetting('BLOG_SOCIALBOOKMARKS_DELICIOUS', ForceIncomingBool('BLOG_SOCIALBOOKMARKS_DELICIOUS', 0), 0);
                $this->ConfigurationManager->DefineSetting('BLOG_SOCIALBOOKMARKS_STUMBLE', ForceIncomingBool('BLOG_SOCIALBOOKMARKS_STUMBLE', 0), 0);
                $this->ConfigurationManager->DefineSetting('BLOG_SOCIALBOOKMARKS_TECHNORATI', ForceIncomingBool('BLOG_SOCIALBOOKMARKS_TECHNORATI', 0), 0);
		$this->DelegateParameters['ConfigurationManager'] = &$this->ConfigurationManager;
                // And save everything
		if ($this->ConfigurationManager->SaveSettingsToFile($SettingsFile)) {
		    header('location: '.GetUrl($this->Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=Blog&Success=1'));
		} else {
		    $this->PostBackAction = 'Blog';
		}
            }
        }
    }

    function Render()
    {
        if ($this->IsPostBack)
	{
            $this->PostBackParams->Clear();
	    if ($this->PostBackAction == 'Blog')
            {
                // Load the category selector
		$cm = $this->Context->ObjectFactory->NewContextObject($this->Context, 'CategoryManager');
		$CategoryData = $cm->GetCategories(0, 1);
		$cs = $this->Context->ObjectFactory->NewObject($this->Context, 'Select');
		$cs->Name = 'BLOG_CATEGORY_ID';
		$cs->CssClass = 'CategorySelect';

		$cat = $this->Context->ObjectFactory->NewObject($this->Context, 'Category');
		$LastBlocked = -1;
		while ($Row = $this->Context->Database->GetRow($CategoryData)) {
			$cat->Clear();
			$cat->GetPropertiesFromDataSet($Row);
			if ($cat->Blocked != $LastBlocked && $LastBlocked != -1) {
				$cs->AddOption("-1", "---", " disabled=\"true\"");
			}
			$cs->AddOption($cat->CategoryID, $cat->Name);
			$LastBlocked = $cat->Blocked;
		}
		$cs->SelectedValue = ForceIncomingInt('BLOG_CATEGORY_ID', $this->Context->Configuration['BLOG_CATEGORY_ID']);

                $this->PostBackParams->Set('PostBackAction', 'ProcessBlog');
                $ThemeFilePath = $this->Context->Configuration['EXTENSIONS_PATH'].'Blog/theme/Theme.BlogForm.php';
                include($ThemeFilePath);
            }
        }
    }
}
?>
